﻿using GameCreator.Core.Hooks;
using Gp.Scripts.Core;
using Helpers;
using UnityEngine;

namespace Gp.Scripts.Character {
    public abstract class BaseCharacter : GameCreator.Characters.Character, IGUID {
        // PROPERTIES: ----------------------------------------------------------------------------
        public Vector3 direction = Vector3.zero;

        // INITIALIZERS: --------------------------------------------------------------------------

        protected override void Awake() {
            base.Awake();
            // todo: 使用配置
            this.characterLocomotion.runSpeed = 5.6f;
        }
        

        
        // UPDATE: --------------------------------------------------------------------------------

        protected virtual void Update() {
            if (!Application.isPlaying) return;

            if (!IsControllable()) {
                direction = Vector3.zero;
            }


            CharacterUpdate();
        }


        // OTHER METHODS: -------------------------------------------------------------------------
        protected Camera cacheCamera;

        protected Camera GetMainCamera() {
            if (HookCamera.Instance != null) return HookCamera.Instance.Get<Camera>();
            if (this.cacheCamera != null) return this.cacheCamera;

            this.cacheCamera = ObjectHelper.MainCamera;
            if (this.cacheCamera != null) {
                return this.cacheCamera;
            }

            this.cacheCamera = FindObjectOfType<Camera>();
            if (this.cacheCamera != null) {
                return this.cacheCamera;
            }

            Debug.LogError(ERR_NOCAM, gameObject);
            return null;
        }

        public bool CanRun {
            get => this.characterLocomotion.canRun;
            set => this.characterLocomotion.canRun = value;
        }


        /// <summary>
        /// 0 => 静止
        /// 1 => Walk
        /// 2 => Run
        /// 3 => Sprint
        /// </summary>
        public void SetMovementMode(int level) {
            // this.characterLocomotion.movement
        }

        public string Guid => GetID();
    }
}